package business_opportunity

import (
	"git.mycaigou.com/gfyx/micro-gfyx-api/http_server/controller"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/errcode"
	dto "git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/business_opportunity"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/utils"
	pb "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/business_opportunity"
	"git.mycaigou.com/gfyx/micro-gfyx-api/service/business_opportunity/contact"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/gin-gonic/gin"
)

type ContactController struct {
	controller.BaseController
	contactService contact.ContactServiceIface
}

// NewContactController 构造函数
func NewContactController(
	contactService contact.ContactServiceIface,
) *ContactController {
	return &ContactController{contactService: contactService}
}

// RegisterRouter
// 注册路由
func (c *ContactController) RegisterRouter(g *gin.RouterGroup) {
	// 获取联系人列表
	g.POST("/get-list", c.getContactList)
	// 联系人详情
	g.POST("/detail", c.contactDetail)
	// 获取手机号
	g.POST("/get-mobile", c.getMobile)
	// 合作案例
	g.POST("/cooperation-list", c.cooperationList)
	// Rpa联系人列表
	g.POST("/rpa-contact", c.RpaContact)
	// Rpa联系人手机号
	g.POST("/rpa-contact-mobile", c.RpaContactMobile)

}

func (c *ContactController) getContactList(ctx *gin.Context) {
	request := &dto.ContactListRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		stark.Logger.Errorf(ctx, "参数错误:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "参数错误")
		return
	}

	if len(request.Company) < 1 {
		stark.Logger.Errorf(ctx, "contact/get-list err:%s", "company参数异常")
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "company参数异常")
		return
	}
	rs, err := c.contactService.GetContactList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "contact/get-list err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}

	c.Success(ctx, rs)
}

func (c *ContactController) contactDetail(ctx *gin.Context) {
	request := &dto.ContactDetailRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		stark.Logger.Errorf(ctx, "参数错误:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "参数错误")
		return
	}

	rs, err := c.contactService.ContactDetail(ctx, &pb.ContactDetailRequest{Id: request.Id})
	if err != nil {
		stark.Logger.Errorf(ctx, "contact/detail err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}

	c.Success(ctx, rs)
}

func (c *ContactController) cooperationList(ctx *gin.Context) {
	request := &dto.CooperationListRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		stark.Logger.Errorf(ctx, "参数错误:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "参数错误")
		return
	}

	rs, err := c.contactService.CooperationList(ctx, &pb.CooperationListRequest{
		PhoneNumber: request.PhoneNumber,
		Category:    request.Category,
		Page:        request.Page,
		PageSize:    request.PageSize,
	})
	if err != nil {
		stark.Logger.Errorf(ctx, "contact/cooperation-list err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}

	c.Success(ctx, rs)
}

func (c *ContactController) getMobile(ctx *gin.Context) {
	request := &dto.GetMobileRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		stark.Logger.Errorf(ctx, "参数错误:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_ERROR, "参数错误")
		return
	}

	rs, err := c.contactService.GetMobile(ctx, &pb.GetMobileRequest{
		Id:          request.Id,
		IgnoreCount: request.IgnoreCount,
	})
	if err != nil {
		stark.Logger.Errorf(ctx, "contact/get-mobile err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_ERROR, "接口返回异常")
		return
	}

	c.Success(ctx, rs)
}

func (c *ContactController) RpaContact(ctx *gin.Context) {
	req := &dto.GetRpaContactRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.contactService.GetRpaContact(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "contact/rpa-contact err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, err.Error())
		return
	}
	c.Success(ctx, res)
}

func (c *ContactController) RpaContactMobile(ctx *gin.Context) {
	req := &dto.GetRpaContactMobileRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.contactService.GetRpaContactMobile(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "contact/rpa-contact-mobile err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, err.Error())
		return
	}
	c.Success(ctx, res.Data)
}
